{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "import cv2\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_model = tf.keras.models.load_model('mnist.h5')\n",
    "#调用模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('3.png',0)\n",
    "#读取图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1ca56b9ae80>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAALtklEQVR4nO3dX4hc5R3G8eepjQmNBpKmCTGGaCWgUmyUJa1NLRapibmJUizmQlIQ4oWCghcVK+iFF6FUbS+KEGswKVYRVMxFqIYgRKmIa0jzp2sbK6nGDUlDLhIr5o/+erEnZU12zqxzzpkzye/7gWVmzju752HIk3Nm3pl5HRECcP77RtsBAPQHZQeSoOxAEpQdSIKyA0l8s587u9BTY5qm93OXQCqf6786Ecc90VilstteLun3ki6Q9MeIWFt2/2marh/4piq7BFDindjacazn03jbF0j6g6RbJF0taZXtq3v9ewCaVeU5+xJJH0TEhxFxQtILklbWEwtA3aqUfb6kj8fd3l9s+wrba2wP2x4+qeMVdgegiipln+hFgLPeexsR6yJiKCKGpmhqhd0BqKJK2fdLWjDu9qWSR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Y2I6yTdIume4nQVkzOpZbz7ZYJlxgdCr8ufV9VG2fdLWjDu9qWSRlvIMaGIGC0uD0l6RYO3FPXB0yvoFpeHWs7zf4O0jPdEy4xrAB67Npc/b6Ps70paZPty2xdKukPSphZynMX29OKFE9meLulmDd5S1JskrS6ur5b0aotZvmJQlvHutMy4Wn7sWl/+PCL6/iNphcZekf+XpF+3kaFDru9K+lvxs6ftbJKe19hp3UmNnRHdJenbkrZK2ltczhqgbH+StEvSTo0Va15L2X6ssaeGOyXtKH5WtP3YleTqy+PG22WBJHgHHZAEZQeSoOxAEpQdSIKyA0lQdiAJyg4k8T99P57+5al8yAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.resize(img,(28,28))\n",
    "img = img.reshape(1,28,28,1)\n",
    "img = img/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "predict = new_model.predict(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.9374178e-07, 4.0664068e-05, 4.3683460e-05, 9.9903995e-01,\n",
       "        5.0486175e-08, 1.3586319e-04, 1.6269293e-09, 6.7247183e-04,\n",
       "        3.3387743e-05, 3.3583085e-05]], dtype=float32)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(predict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
